package com.bobo.base.viewmodel;


import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.bobo.base.api.BaseApiService;
import com.bobo.base.network.NewDefaultObserver;
import com.bobo.base.network.api.IdeaApi;
import com.bobo.base.network.base.BasicResponse;
import com.bobo.base.network.util.RxUtils;
import com.trello.rxlifecycle3.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：公用ViewModel
 * 作者: sheng
 * 日期: 2022/4/1 13:38
 */
public class CommonViewModel extends BaseViewModel {

    protected LifecycleTransformer lifecycleTransformer;

    public CommonViewModel(LifecycleTransformer lifecycleTransformer) {
        this.lifecycleTransformer = lifecycleTransformer;
    }

    /**
     * 获取浏览记录
     */

    public MutableLiveData<String> tapLiveData = new MutableLiveData<>();
    public void tapPoint(String targetId) {
        if (TextUtils.isEmpty(targetId))
            return;

        Map<String, String> map = new HashMap<>();
        map.put("targetId", targetId);
        IdeaApi.getInstance().create(BaseApiService.class).tapPoint(map)
                .compose(RxUtils.applySchedulers())
                .compose(lifecycleTransformer)
                .subscribe(new NewDefaultObserver<BasicResponse>() {
                    @Override
                    public void onSuccess(BasicResponse response) {
                        tapLiveData.postValue(response.getMsg()+ "成功");
                    }

                    @Override
                    public void onFail(Throwable e) {
                        tapLiveData.postValue(e.getMessage());
                    }

                    @Override
                    protected void onCodeFail(String message) {
                        super.onCodeFail(message);
                        tapLiveData.postValue(message);
                    }
                });
    }

}
